import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _toPath from "lodash/toPath";
import _set from "lodash/set";
import _forEach from "lodash/forEach";
import { attributeNameProp } from "../config";
var attrFlagField = 'attributeFlag';
var templateNoConfigAttr = ['_token', '_tls', 'objectVersionNumber', 'enabledFlag', 'templateId', 'templateCode', 'templateType', 'description', 'orderSeq', 'fields', // 后端传过来的数据
'rootNode', 'childNode'];
var templateNoConfigAttrMap = {};

_forEach(templateNoConfigAttr, function (attrName) {
  templateNoConfigAttrMap[attrName] = true;
});
/**
 * 判断是否是 attr
 * @param {Object} attr - 判断是不是 attribute 的对象
 * @returns {boolean}
 */


function isAttribute(attr) {
  return attr[attrFlagField];
}
/**
 * 将 templates config 的值 设置到 头数据中
 */


export function dealConfig(templates) {
  _forEach(templates, function (template) {
    _forEach(template.config, function (attr) {
      var _objectSpread2;

      var attrPath = _toPath(attr);

      _set(template, attrPath, _objectSpread(_objectSpread({}, attr), {}, (_objectSpread2 = {}, _defineProperty(_objectSpread2, attrFlagField, true), _defineProperty(_objectSpread2, attributeNameProp, attrPath[attrPath.length - 1]), _objectSpread2)));
    });

    _forEach(template.fields, function (field) {
      _forEach(field.config, function (attr) {
        var _objectSpread3;

        var attrPath = _toPath(attr);

        _set(field, attrPath, _objectSpread(_objectSpread({}, attr), {}, (_objectSpread3 = {}, _defineProperty(_objectSpread3, attrFlagField, true), _defineProperty(_objectSpread3, attributeNameProp, attrPath[attrPath.length - 1]), _objectSpread3)));
      });

      delete field.config;
    });

    delete template.config;
  });
}
/**
 * 将 templates 行数据的值 重新设置到 行数据中
 * @param templates
 */

export function parseConfig(templates) {
  _forEach(templates, function (template) {
    _forEach(template, function (attr, attrK) {
      if (!templateNoConfigAttrMap[attrK]) {
        setConfig(template, attrK, attr);
      }
    });
  });
}

function setConfig(component, attrK, attr) {
  if (isAttribute(attr)) {
    component[attrK] = attr;
  } else {
    _forEach(attr, function (v, k) {
      setConfig(component, "[".concat(attrK, "][").concat(k, "]"), v);
    });
  }
}